#############################################################################################
## Replication code for:                                                                   ##
## A Male Hostility Spiral? Polarized Communication among Political Elites on Social Media ##
## Step 4.                                                                                 ##
#############################################################################################

library(googleLanguageR)
options(scipen=999)

gl_auth("google_key.json") # requires access to Google API

pb <- progress_bar$new(
  format = "  [:bar] :percent :current/:total (:eta)",
  total = nrow(data1), clear = FALSE, width = 60
)

for (i in 1:nrow(data1)){
  suppressMessages(temp <- gl_translate(data1$text[i]))
  data1$text_en[i] <- temp$translatedText
  data1$language[i] <- temp$detectedSourceLanguage
  pb$tick()
}

pb <- progress_bar$new(
  format = "  [:bar] :percent :current/:total (:eta)",
  total = nrow(data2), clear = FALSE, width = 60
)

for (i in 1:nrow(data2)){
    suppressMessages(temp <- gl_translate(data2$text[i]))
    data2$text_en[i] <- temp$translatedText
    data2$language[i] <- temp$detectedSourceLanguage
    pb$tick()
}

annotated_data <- sample(data1,1467)
annotated_data <- annotated_data[c("id","id_target","language","text_en","Receiver")]

data$annotated <- 0
data[data$id_target %in% annotated_sample$id_target,]$annotated <- 1

write.csv(data1,"data1.csv",row.names=F)
write.csv(data2,"data2.csv",row.names=F)
write.csv(annotated_data,"annotated_data.csv",row.names=F)